#!/bin/env python
"""Simple packet API for network routing.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA


The packet class packages the data into simple packets for
communication.  Sensors send each other packets of data. The packet
class defines a framework for these communications.
"""

__author__  = "Sarah Mount <s.mount@wlv.ac.uk>"

DEBUG = True

class Packet:
    """Definition of a Packet of data
    Sensors send each other Packets of data. Subclass this to hold whatever you like.
    Note that the Ether sets an RSSI in here, as well as a
    time_sent attribute.
    """
    def __init__(self, sender_id, receiver_id, msg):
        """Create a new data packet.
        
        This creates a simple packet object holding the information required to send a message.
        The packet holds details of the sender, destination and the message.  

        @type sender_id: str
        @param sender_id:  The ID of the sending sensor
        @type receiver_id: str
        @param receiver_id: The ID of the receiving sensor
        @type msg: any
        @param msg: The message to be sent
        """
        self.sender_id = sender_id
        self.receiver_id = receiver_id
        self.msg = msg
        return
        
    def __repr__(self):
        """Format a packet for printing.
        Standard method returns a string object showing the packet details

        @rtype: str
        """
        if self.__dict__.has_key('rssi') and self.__dict__.has_key('time_sent'):

            info = (self.sender_id,
                self.receiver_id,
                self.msg, self.rssi,
                self.time_sent)
            return ('Packet(Sender=%s, Receiver=%s, Msg=%s, RSSI=%f, TimeSent=%.3f)' % info)
        else :
            info = (self.sender_id, self.receiver_id, self.msg)
            return ('Packet(Sender=%s, Receiver=%s, Msg=%s)' % info)



if __name__ == '__main__': # Only for unit testing
    pass
